﻿package pages {
	import com.gaiaframework.templates.AbstractPage;
	import com.gaiaframework.events.*;
	import com.gaiaframework.debug.*;
	import com.gaiaframework.api.*;

	import flash.display.*;
	import flash.events.*;

	import events.NBEvent;

	import gs.*;

	public class RetroPage extends AbstractPage {
		private var tran : MovieClip;
		private var releaseGaia : Function;		

		public function RetroPage() {
			super();
			alpha = 0;
		}

		override public function transitionIn() : void {
			init();
			super.transitionIn();
			TweenLite.to(this, 0.3, {alpha:1, onComplete:transitionInComplete});
		}

		override public function transitionOut() : void {
			super.transitionOut();
			TweenLite.to(this, 0.3, {alpha:0, onComplete:transitionOutComplete});
		}

		override public function transitionInComplete() : void {
			super.transitionInComplete();
		}
		
		override public function transitionOutComplete() : void {
			super.transitionOutComplete();
		}

		private function onBeforeTransitionIn(event : GaiaEvent) : void {
			//判断是否为直接跳转，如果是直接跳转，则页面暴白后再调用Gaia事件流；否则直接调用Gaia事件流
			if(Model.instance.dir != Model.JUMP) {
				release();
			} else {
				TweenLite.to(assets.app.content, .8, {tint:0xffffff, onComplete:release});
			}
		}
		
		//重新调用Gaia事件流
		private function release() : void {
			releaseGaia(true);
		}

		private function init() : void {
			//跳转页面后,当目标页面加载结束,调用TransitionIn之前调用onBeforeTransitionIn
			releaseGaia = Gaia.api.beforeTransitionIn(onBeforeTransitionIn, true);
			
			assets.trans.alpha = 0;
			assets.trans.visible = true;
			TweenLite.to(assets.trans, 0.2, {alpha:1});
			
			if(Model.instance.dir == Model.BACKWARD) {
				tran = assets.trans.content.getClass("B");
			} else {
				tran = assets.trans.content.getClass("A");
			}
			assets.trans.addEventListener(NBEvent.TRANSITION_END, onTransitionEnd);
			
			if(Model.instance.dir == Model.JUMP) {
				tran.gotoAndPlay(Model.instance.getJumpFrame(tran));
			} else {
				tran.play();
			}
		}

		private function onTransitionEnd(e : Event) : void {
			assets.trans.removeEventListener(NBEvent.TRANSITION_END, onTransitionEnd);
			assets.trans.visible = false;
			assets.app.visible = true;
			assets.app.addEventListener(NBEvent.TRANSITION_FORWARD, onForwarHandler);
			assets.app.addEventListener(NBEvent.TRANSITION_BACKWARD, onBackwarHandler);
		}

		private function onForwarHandler(e : Event) : void {
			Model.instance.dir = Model.FORWARD;
			Gaia.api.goto("index/nav/" + Model.getPagesWithId(Model.instance.pid));
		}

		private function onBackwarHandler(e : Event) : void {
			Model.instance.dir = Model.BACKWARD;
			Gaia.api.goto("index/nav/" + Model.getPagesWithId(Model.instance.pid));
		}
	}
}
